package com.dly.hope.listener;

import com.dly.hope.constant.RedisConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @author linyu.dai
 * @project myserve
 * @description 监听信息做异步
 * @creat 2022/11/8 10:28:02
 */
@Configuration
public class RedisListener {

    @Bean
    public MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver){
        return new MessageListenerAdapter(redisReceiver, "onMessage");
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter adapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        //监听发送信道
        container.addMessageListener(adapter, new PatternTopic(RedisConstant.MESSAGE_CHANNEL));
        return container;
    }

}
